#!user/bin/python
#coding: utf-8
import re
from ParserUtil import *
class MekongInfo:
    src_html = ""
    pre_process_content = []
    id = 0
    title = ""
    publish_date = ""
    record_date = ""
    href = ""
    source = ""
    text = ""
    keywords = ""

    def pre_process(self,srchtml):
        self.src_html = srchtml
        self.pre_process_content = re.findall(r"<h3>(.*?)</p>", srchtml, re.S) or ['']

    def get_title(self, code):
        '''get title'''
        arr = re.findall(r"<a.*?>(.*?)</a>", code, re.S) or ['']
        self.title = arr[0].strip()

    def get_href(self, code):
        arr = re.findall(r"href=\"(.*?)\"", code, re.S) or ['']
        self.href = arr[0].strip()

    def get_source(self,code):
        arr = re.findall(r"<p.*?>(.*?)<br/>", code, re.S) or ['']
        self.source = arr[0].strip()

    def get_publish_date(self,code):
        arr = re.findall(r"<br/>(.*?)<br/>", code, re.S) or ['']
        if(arr[0]):
            parser = ParserUtil()
            self.publish_date = parser.parseDate(arr[0].strip())

    def get_record_date(self,code):
        arr = re.findall(r"Date:(.*)", code, re.S) or ['']
        if(arr[0]):
            parser = ParserUtil()
            self.record_date = parser.parseDate(arr[0].strip())

    def get_info_list(self):
        ''''''
        reslist = []
        for each in self.pre_process_content:
            minfo = MekongInfo()
            minfo.get_title(each)
            minfo.get_href(each)
            minfo.get_source(each)
            minfo.get_publish_date(each)
            minfo.get_record_date(each)
            reslist.append(minfo)
        return reslist

    def pack_info_from_query(self,datalist):
        reslist = []
        if datalist:
            for each in datalist:
                minfo = MekongInfo()
                minfo.id = each[0]
                minfo.title = each[1]
                minfo.href = each[2]
                minfo.source = each[3]
                minfo.publish_date = each[4]
                minfo.record_date = each[5]
                reslist.append(minfo)
        return reslist